﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace ManagoMes.Read.API.Applications.SellStockSearchRead
{
    public class MaterialSearchHandler : IRequestHandler<MaterialSearchCommand, CodePageDTO<MaterialSearchDTO>>
    {
        private readonly IBateRepository<MaterialSearchDTO> material;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="material"></param>
        public MaterialSearchHandler(IBateRepository<MaterialSearchDTO> material)
        {
            this.material = material;
        }
        /// <summary>
        /// 库存物资选择
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<CodePageDTO<MaterialSearchDTO>> Handle(MaterialSearchCommand request, CancellationToken cancellationToken)
        {
            DapperHelper helper = new DapperHelper();
            MaterialSearchDTO materials = new MaterialSearchDTO();
            CodePageDTO<MaterialSearchDTO> dto = new CodePageDTO<MaterialSearchDTO>();
            string conn = helper.MangoMesRead;
            string sql = "SELECT * FROM stockremoval a INNER JOIN  warehouselist b on a.WarehouseId=b.StashId inner join productmanage c on a.productManageNode=c.ProductManageNode";
            var res = material.GetAll(sql, materials, conn).ToList();
            dto.TotalCount = res.Count();
            dto.PageCount = (int)Math.Ceiling(res.Count() * 1.0 / request.pageSize);
            dto.Data = res.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            dto.Code = 0;
            return dto;
        }
    }
}
